package com.tanhua.manage.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.manage.mapper.OprationLogMapper;
import com.tanhua.manage.pojo.Admin;
import com.tanhua.manage.pojo.OprationLog;
import com.tanhua.manage.util.UserThreadLocal;
import com.tanhua.manage.vo.UserFreezeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 保存操作日志类
 */
@Service
public class OprationLogService {

    @Autowired
    private OprationLogMapper oprationLogMapper;

    private final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 保存冻结/解冻日志方法
     * @param freezeVo
     * @param ipAddresses
     * @return
     */
    public Boolean saveOprationLog(UserFreezeVo freezeVo, String ipAddresses){
        try {
            // 保存操作日志
            Admin admin = UserThreadLocal.get();
            // 弹窗详细信息
            Map<String,String> logDetails = new HashMap<>();

            OprationLog oprationLog = new OprationLog();
            oprationLog.setOprationIp(ipAddresses);
            oprationLog.setUserName(admin.getUsername());
            logDetails.put("操作人",admin.getUsername());
            logDetails.put("IP",ipAddresses);
            // 解冻原因为空
            String freezingRangeStr = null;
            if(freezeVo.getReasonsForThawing() == null){
                oprationLog.setOprationDescribe("冻结用户");
                oprationLog.setOprationMessage("冻结【"+freezeVo.getUserId()+"】用户");
                logDetails.put("操作","冻结用户");
                //冻结范围，1为冻结登录，2为冻结发言，3为冻结发布动态
                Integer freezingRange = freezeVo.getFreezingRange();

                switch (freezingRange){
                    case 1:
                        freezingRangeStr="冻结登录";
                        break;
                    case 2:
                        freezingRangeStr="冻结发言";
                        break;
                    case 3:
                        freezingRangeStr="冻结发布动态";
                        break;
                    default:
                        freezingRangeStr="前台未传入范围";
                        break;
                }
                Integer days = freezeVo.getFreezingTime();
                if (freezeVo.getFreezingTime() == 1) {
                    days = 3;
                } else if (freezeVo.getFreezingTime() == 2) {
                    days = 7;
                }
                if(days>0){
                    logDetails.put("描述","将【"+freezeVo.getUserId()+"】用户冻结"+days+"天,"+freezingRangeStr+",原因是："+freezeVo.getFrozenRemarks());
                }else {
                    logDetails.put("描述","将【"+freezeVo.getUserId()+"】用户永久冻结，"+freezingRangeStr+",原因是："+freezeVo.getFrozenRemarks());
                }
            }else {
                oprationLog.setOprationDescribe("解冻用户");
                oprationLog.setOprationMessage("解冻【"+freezeVo.getUserId()+"】用户");
                logDetails.put("操作","解冻用户");
                logDetails.put("描述","将【"+freezeVo.getUserId()+"】用户永久解冻，原因是："+freezeVo.getFrozenRemarks());
            }
            String logDetailsStr = null;
            try {
                logDetailsStr = MAPPER.writeValueAsString(logDetails);
            } catch (JsonProcessingException e) {
                e.printStackTrace();
            }
            oprationLog.setLogDetails(logDetailsStr);
            oprationLog.setCreateDate(System.currentTimeMillis());
            //保存到数据库
            oprationLogMapper.insert(oprationLog);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
